package com.ncs.cns.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ImageButton;

import com.ncs.cns.R;
import com.ncs.cns.business.service.LoginService;
import com.ncs.cns.business.to.UserNcs;
import com.ncs.cns.business.util.StringUtils;
import com.ncs.cns.framework.Constant;

public class LoginActivity extends BaseActivity {
	
	private EditText userId = null;
	private EditText userPwd = null;
	private ImageButton mClear1;
	private ImageButton mClear2;
	
	private Button loginBtn = null;
	private Button registerBtn = null;
	private Button findPwdBtn = null;
	
	private CheckBox checkBox;//记住密码

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		
		userId = (EditText)this.findViewById(R.id.userId);
		userPwd = (EditText)this.findViewById(R.id.userPwd);
		
        //登陆按钮
        loginBtn = (Button)this.findViewById(R.id.loginBtn);
        loginBtn.setOnClickListener(new LoginOnClickListener());
        
        //注册按钮
        registerBtn = (Button)this.findViewById(R.id.registerBtn);
        registerBtn.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent(LoginActivity.this,RegisterActivity.class);
				startActivity(intent);
				finish();
			}
		});
        
        //找回密码
        findPwdBtn = (Button)this.findViewById(R.id.findPwdBtn);
        findPwdBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				//TODO
			}
		});
        

        //记住密码
		checkBox = (CheckBox) findViewById(R.id.login_check);
        checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				SharedPreferences  sp = getSharedPreferences(Constant.SHAREPATH, Context.MODE_PRIVATE);
				Editor editor = sp.edit();
				editor.putBoolean(Constant.USERPASSWORDREMEMBER_COOKIE, isChecked);
				editor.commit();
				application.setUserPasswordRemember(isChecked);
			}
		});
        
        if(application.getUser() != null){
        	userId.setText(application.getUser().getUserId());
        	userPwd.setText(application.getUser().getPassword());
        }
        
        mClear1 = (ImageButton)findViewById(R.id.clearUserId);
        mClear1.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				userId.setText("");
			}
		});
        
        mClear2 = (ImageButton)findViewById(R.id.clearPwd);
        mClear2.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				userPwd.setText("");
			}
		});
        
        setUserIdTextChangeListener(userId, mClear1);
        setUserPwdTextChangeListener(userPwd, mClear2);
	}

	public class  LoginOnClickListener implements View.OnClickListener{
		public void onClick(View arg0) {
			String userIdStr = userId.getText().toString();
			String password = userPwd.getText().toString();
			
			if(TextUtils.isEmpty(userIdStr)){
				showToast(R.string.error_user_id_empty);
				userId.setFocusable(true);
				userId.requestFocus();
				return;
			}
			
			if (!StringUtils.isNumberLetter(userIdStr)) {
				showToast(R.string.error_user_id_expr);
				userId.setFocusable(true);
				userId.requestFocus();
				return;
			}
			
			if (StringUtils.strLength(userIdStr)<3) {
				showToast(R.string.error_user_id_min_length);
				userId.setFocusable(true);
				userId.requestFocus();
				return;
			}
			
			if (StringUtils.strLength(userIdStr)>20) {
				showToast(R.string.error_user_id_max_length);
				userId.setFocusable(true);
				userId.requestFocus();
				return;
			}
			
			if (TextUtils.isEmpty(password)) {
				showToast(R.string.error_pwd);
				userPwd.setFocusable(true);
				userPwd.requestFocus();
				return;
			}
			
			if (StringUtils.strLength(password)<6) {
				showToast(R.string.error_pwd_min_length);
				userPwd.setFocusable(true);
				userPwd.requestFocus();
				return;
			}
			
			if (StringUtils.strLength(password)>20) {
				showToast(R.string.error_pwd_max_length);
				userPwd.setFocusable(true);
				userPwd.requestFocus();
				return;
			}
			
			if(commService.isNetworkAvailable()){
				//判断用户是否存在
				boolean isValidUserAndPwd = LoginService.isValidUserAndPwd(userIdStr, password);
				if(isValidUserAndPwd){
					SharedPreferences  sp = getSharedPreferences(Constant.SHAREPATH, Context.MODE_PRIVATE);
					Editor editor = sp.edit();
					editor.putString(Constant.USERID_COOKIE, userIdStr);
					boolean userPwdRemember = sp.getBoolean(Constant.USERPASSWORDREMEMBER_COOKIE, true);
					if(userPwdRemember){
						editor.putString(Constant.USERPASSWORD_COOKIE, password);
					}
					editor.commit();
					UserNcs user = new UserNcs();
					user.setUserId(userIdStr);
					user.setPassword(password);
					application.setUser(user);
					
					Intent intent = new Intent(LoginActivity.this, CircuSeeActivity.class);
					startActivity(intent);
				}else{
					showToast(R.string.error_userid_pwd);
				}
				
			}else{
				showToast(R.string.error_network);
			}
		}        	
	}
	
}
